<?php

namespace App\Exports;

use App\Models\User\LiPei;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;

class LiPeiExport extends DefaultValueBinder implements FromArray,WithCustomValueBinder
{

    private $conditions;

    public function __construct(array $conditions) {
        $this->conditions = $conditions;
    }

    public function array():array  {
        $data = [
             [
                '导入编号',
                //'立案号',
                '保单号',
                '投保人',
                '被保险人',
                //'出险时间',
                '出险地点',
                '车号',
                 '事故原因',
                 '未决金额',
                '是否销案',
                '账号',
                '导入时间',
            ]
        ];



        $records = (new LiPei())->getLst($this->conditions)->with('getGuardUser:id,name')->get();
        foreach ($records as $record) {
            unset($record->is_auto_check);
            $record->account = $record->getGuardUser->name;
            unset($record->case_number);
            unset($record->chu_xian_date);
            unset($record->getGuardUser);
            $data[] = $record->toArray();
        }
        return $data;
    }

    public function bindValue(Cell $cell, $value) {
        if (strlen($value) > 10) {
            $cell->setValueExplicit($value, DataType::TYPE_STRING);
            return true;
        }
        return parent::bindValue($cell, $value);
    }

}
